package main

import (
	"fmt"
	"sync"
	"time"
)

func formattings(s string) (ti time.Time, er error) {
	if len(s) <= 3 || s == "" {
		s = time.Now().Format("2006-01-02 15:04:05")
	}
	var forttmed = time.Now().Format("2006-01-02 15:04:05")
	ti, er = time.Parse(s, forttmed)
	return ti, er
}
func timeaddremote() {
	var time1 = time.Now().Add(time.Hour * 24)
	time1 = time.Now().Add(time.Millisecond * 1000)

	var time2 = time.Now().Add(time.Millisecond * -1000)
	var time3 = time.Now().Add(time.Minute * -1000)
	fmt.Printf("time1:%s,time2:%v,time3:%v", time1, time2, time3)
}

func timeComparison() {
	fmt.Println()
	var time1 = time.Now()
	var time2 = time.Now().Add(time.Millisecond * 1000)
	if time1.Before(time2) {
		fmt.Printf("time1 before time2 .time1: %d,time2:%d\n", time1, time2)
	}
	if time1.After(time2) {
		fmt.Printf("time1 after time2 .time1: %d,time2:%d \n", time1, time2)
	}
}
func createTimeTicker() {
	fmt.Println("Timer ti expired!!!%d", time.Now().Format("2006-01-02 15:04:05"))
	var ti = time.NewTicker(time.Second * 2)
	<-ti.C
	fmt.Println("Timer ti expired!!!")
	fmt.Println("Timer ti expired!!!%d", time.Now().Format("2006-01-02 15:04:05"))
}
func createTimeTickerTreade() {
	fmt.Println("========createTimeTickerTreade==========")
	var ti = time.NewTicker(time.Second * 1)
	go func() {
		<-ti.C
		for ti := range ti.C {
			fmt.Println("Tick it treade g func:", ti)
		}
	}()
	time.Sleep(time.Second * 10)
	ti.Stop()
}

func tickersync() {
	var wg sync.WaitGroup

	wg.Add(1)                     // 增加一个 WaitGroup
	go createTimeTicker3sync(&wg) // 启动 goroutine

	time.Sleep(time.Second * 5) // 休眠 5 秒
	wg.Wait()                   // 等待 goroutine 完成
}
func createTimeTicker3sync(wg *sync.WaitGroup) {
	defer wg.Done() // 当函数结束时，通知 WaitGroup
	fmt.Println("========createTimeTickerTreade==========")
	var ti = time.NewTicker(time.Second * 1)
	defer ti.Stop() // 确保在函数结束时停止定时器

	for {
		select {
		case t := <-ti.C: // 等待 tick
			fmt.Println("Tick it treade g func:", t)
		}
	}
}
